package com.juyu.miliao.api.dao.mysql;

import com.juyu.miliao.api.domain.WhitePacketDynamicSourceInfo;
import com.juyu.miliao.api.domain.dto.WhitePacketDynamicInfoDTO;
import com.juyu.miliao.api.domain.vo.DynamicSourceInfoVO;
import com.juyu.miliao.api.domain.vo.WhitePacketDynamicSourceInfoVO;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xuanqicheng
 * @create 2019-21-10:14
 */
@Repository
public class WhitePacketDynamicSourceInfoDao {
    @Autowired
    private SqlSession sqlSession;

    public void addDynamicSourceInfos(Long dynamicId, WhitePacketDynamicInfoDTO dynamicInfoDTO) {

        List<String> sourceList = dynamicInfoDTO.getDynamicSourceUrls();
        if (sourceList.size() > 0) {
            List<WhitePacketDynamicSourceInfo> dynamicSourceInfos = new ArrayList<>();
            for (String url : sourceList) {
                if (!StringUtils.isEmpty(url)) {
                    WhitePacketDynamicSourceInfo dynamicSourceInfo = new WhitePacketDynamicSourceInfo()
                            .setDynamicId(dynamicId)
                            .setDynamicSourceUrl(url)
                            .setVideoImgUrl(dynamicInfoDTO.getVideoImgUrl())
                            .setWidth(dynamicInfoDTO.getWidth())
                            .setHeight(dynamicInfoDTO.getHeight())
                            .setVideoTime(dynamicInfoDTO.getVideoTime());

                    dynamicSourceInfos.add(dynamicSourceInfo);

                }

            }
            sqlSession.insert("addWhitePacketDynamicSourceInfos",dynamicSourceInfos);
        }


    }

    public List<WhitePacketDynamicSourceInfoVO> selectDynamicSourcesByDynamicId(Long dynamicId) {
        return sqlSession.selectList("selectWhitePacketDynamicSourcesByDynamicId",dynamicId);
    }
}
